<template>
  <el-row type="flex" class="row-bg" justify="center">
    <el-col :xl="6" :lg="7">
      <h2>欢迎来到VueAdmin管理系统</h2>
    </el-col>
    <el-col :span="1">
      <el-divider direction="vertical"></el-divider>
    </el-col>
    <el-col :xl="6" :lg="7">
      <el-form :model="loginForm" status-icon :rules="rules" ref="loginForm" label-width="80px">
        <el-form-item label="账号" prop="username" style="width: 380px">
          <el-input type="text" v-model="loginForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password" style="width: 380px;">
          <el-input type="password" v-model="loginForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="code" style="width: 380px;">
          <el-input v-model="loginForm.code" style="width: 200px; float: left" maxlength="5"></el-input>
          <el-image class="captchaImg" :src="captchaImg" @click="getCaptchaImg"></el-image>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('loginForm')">登录</el-button>
          <el-button @click="resetForm('loginForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<script>
import qs from 'qs';
export default {
  name: "Login",
  data() {
    return {
      captchaImg: null,
      loginForm: {
        username: '',
        password: '',
        code: '',
        token: ''
      },
      rules: {
        username: [
          {required: true, message: '请输入账号', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'}
        ],
        code: [
          {required: true, message: '请输入验证码', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.post('/login?' + qs.stringify(this.loginForm)).then(res => {
            const jwt = res.headers['authorization']
            console.log(jwt)
            console.log(this)
            this.$store.commit('SET_TOKEN', jwt)
            this.$router.push('/index')
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    getCaptchaImg() {
      console.log(1111)
      this.$axios.get('/captcha').then(res => {
        console.log(2222)
        console.log(res)
        this.loginForm.token = res.data.result.token
        this.captchaImg = res.data.result.captchaImg
        this.loginForm.code = ''
      })
    }
  },
  created() {
    this.getCaptchaImg()
  }
}
</script>

<style scoped>
  .el-col {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    text-align: center;
  }
  .el-row {
    height: 100%;
    background-color: #fafafa;
    padding-top: 20px;
  }
  .el-divider {
    height: 230px;
  }
  .captchaImg {
    float: left;
    margin-left: 8px;
    border-radius: 4px;
    width: 92px;
    height: 40px;
  }
</style>
